* Create variables

* Measures of log import share:
gen ratio = log(importshare) // JN
gen ratio2 = log(importshare2) // priority to WIOD/ OECD
gen ratioC = log(importshareC) // consumption import share, JN priority
gen ratioC2 = log(importshareC2) // consumption import share, WIOD/OECD priority

* Quadratic time trend:	
gen year2 = year^2

* Log Real GDP
gen RGDP = RGDP1
replace RGDP = RGDP2 if countryname=="Colombia" | countryname=="Mexico"
gen lRGDP = log(RGDP)

* RER for devaluation identification (CPI-based)
gen rer_cpi= e7
replace rer_cpi =e8 if ccode=="SVK" | ccode=="FIN" | ccode=="ESP"  | ccode=="SVN " // CPI not available
replace rer_cpi =e1 if  ccode=="ITA"	
gen logrer_cpi = log(rer_cpi)

* RER displayed in Figure 2
gen     rer = e8 if ccode=="FIN" |  ccode=="ITA"  | ccode=="KOR" | ccode=="MYS" | ccode=="PHL" | ccode=="ROM"| ccode=="RUS" | ccode=="ESP"   | ccode=="SWE" | ccode=="THA"  // PPI
replace rer = e9 if ccode=="IND" // WPI
replace rer = e9 if ccode=="ITA" & year<=1988 // switch to WPI because lack of PPI data pre devaluation
replace rer = e7 if ccode=="ARG" | ccode=="AUS" | ccode=="BRA" | ccode=="KHM" | ccode=="CHL" | ccode=="CHN"  | ccode=="DNK" | ccode=="ISL" | ccode=="IDN"  | ccode=="ISR"  | ccode=="MEX"  | ccode=="POL" | ccode=="ZAF" | ccode=="TUR"  // CPI
replace rer = e9 if ccode=="IDN" & year>=1993 // WPI not available for 1979 episode 
gen lRER = log(rer)
// Note: Using the CPI-based RER gives very similar results for Figures 2 and 3, as well as Table 2 (see Figure2.do and PVSQ.do for code lines to create these alternative versions).
	
* Effective RER from IFS (REER)
gen lRER1 = log(e1) // used in local projection analysis

* Nominal exchange rate
* Rely on nominal exchange rate in domestic currency per USD, period average (E4).
* When not available, use the end of period measure (E1).
gen er = 1/E4 // Express it in terms of USD per domestic currency
replace er = 1/E1 if E4==.

* Choice of Price Index for Table B2 in Online Appendix
gen P=""		
replace P="PPI"   if ccode=="FIN" |  ccode=="ITA"  | ccode=="KOR" | ccode=="MYS" | ccode=="PHL" | ccode=="ROM"| ccode=="RUS" | ccode=="ESP"   | ccode=="SWE" | ccode=="THA"  
replace P="WPI"   if ccode=="IND"
replace P="WPI"   if ccode=="ITA" & year<=1988 // switch to WPI because lack of PPI data pre devaluation
replace P="CPI"   if ccode=="ARG" | ccode=="AUS" | ccode=="BRA" | ccode=="KHM" | ccode=="CHL" | ccode=="CHN"  | ccode=="DNK" | ccode=="ISL" | ccode=="IDN"  | ccode=="ISR"  | ccode=="MEX"  | ccode=="POL" | ccode=="ZAF" | ccode=="TUR"   

* Country id 
egen id = group(ccode)

*. Declare panel
xtset id year, yearly

* Variables to identify events
gen gRER = (rer_cpi - l.rer_cpi)/l.rer_cpi // CPI RER
gen gER = (er - l.er)/l.er // growth in nominal exchange rate
gen acceER= d.gER // change in rate of depreciation relative to previous year

* Relative spending
gen MIMD   = importshare/(1-importshare)
gen MIMD2  = importshare2/(1-importshare2)
gen lMIMD  = log(MIMD)
gen lMIMD2 = log(MIMD2)

* Relative foreign sales
gen SXSD   = exportshare/(1-exportshare)
gen lSXSD  = log(SXSD)
